<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!-- $parent 拿到父组件 -->
    <!-- $root拿到根vue对象 -->
    <div id="hello">
        <cpn></cpn>
    </div>
    <template id="cpn">
        <div>
            子组件
            <button @click='getParent'>子组件按钮</button>
            <son></son>
        </div>
    </template>
    <template id="cpn2">
        <div>
            孙子组件
            <button @click='getParent2'>孙子组件按钮</button>
        </div>
    </template>


    <script src="../../vue.js"></script>
    <script>
        const son = {
            template: '#cpn2',
            methods: {
                getParent2() {
                    console.log(this.$parent.name);
                    console.log(this.$root);
                    console.log(this.$root.age);
                }
            }
        }
        const vue01 = new Vue({
                        el: '#hello',
                        data: {
                            age: 23
                        },
                        methods: {
                            rootMethod() {
                                console.log('rootmethod');
                            }
                        },
                        // 根组件里面的第一个组件
                        components: {
                            cpn: {
                                template: '#cpn',
                                data() {
                                    return {
                                        name: '父组件数据'
                                    }
                                },
                                methods: {
                                    getParent() {
                                        //拿到父组件的对象
                                        console.log(this.$parent.age);
                                        // $拿到根组件的对象
                                        console.log(this.$root);
                                        this.$root.rootMethod();
                                    }
                                },
                                components: {
                                    son,
                                },
                            },
                        },
           
                    
            })
    </script>
</body>

</html>